<?php

namespace app\supplier\model\stall;

use app\common\model\stall\Apply as ApplyModel;

/**
 * 摊位申请模型
 */
class Apply extends ApplyModel
{
    /**
     * 获取申请列表
     * @noinspection PhpUndefinedMethodInspection
     */
    public function getList($search,$shop_id=0)
    {
        // 获取列表数据
        return $this->alias('apply')
            ->field('apply.*, user.nickName')
            ->when(isset($search['reg_date'][0]) && $search['reg_date'][0], function ($query) use ($search) {
                $query->whereTime('apply.create_time', 'between', [$search['reg_date'][0], date('Y-m-d 23:59:59', strtotime($search['reg_date'][1]))]);
            })
            ->when(isset($search['real_name']) && $search['real_name'], function ($query) use ($search) {
                $query->where('user.nickName|apply.real_name|apply.mobile|apply.store_name', 'like', '%' . $search['real_name'] . '%');
            })
            ->when(isset($search['cate_name']) && $search['cate_name'], function ($query) use ($search) {
                $query->where('cate_name', $search['cate_name']);
            })
            ->where('shop_supplier_id', $shop_id)
            ->where('apply_status', 20)
            ->leftJoin('user', 'user.user_id = apply.user_id')
            ->order(['apply.create_time' => 'desc'])
            ->paginate($search['list_rows']);
    }

    /**
     * 添加新记录
     */
    public function add($data)
    {
        $data['app_id']      = self::$app_id;
        $data['province_id'] = $data["region_id"][0] ?? 0;
        $data['city_id']     = $data["region_id"][1] ?? 0;
        $data['county_id']   = $data["region_id"][2] ?? 0;
        unset($data["region_id"]);
        return $this->save($data);
    }

    /**
     * 编辑记录
     */
    public function edit($data)
    {
        $data['update_time']   = time();
        $data["audit_status"]  = $data['audit_status'] ?? 10;
        $data['province_id']   = $data["region_id"][0] ?? 0;
        $data['city_id']       = $data["region_id"][1] ?? 0;
        $data['county_id']     = $data["region_id"][2] ?? 0;
        $data['reject_reason'] = trim($data['reject_reason']);
        unset($data["region_id"]);
        unset($data["apply_time"]);
        unset($data["audit_time"]);
        return $this->save($data);
    }


    /**
     * 审核
     * @param $data
     * @return bool
     */
    public function audit($data)
    {
        if ($data['apply_status'] == '30' && empty($data['reject_reason'])) {
            $this->error = '请填写驳回原因';
            return false;
        }
        $this->startTrans();
        try {
            $save_data = [
                'audit_time'    => time(),
                'apply_status'  => $data['apply_status'],
                'reject_reason' => $data['reject_reason'] ?? '',
            ];
            $this->save($save_data);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }

    /**
     * 获取申请中的数量
     */
    public static function getApplyCount()
    {
        return (new static())->where('apply_status', '=', 10)->count();
    }
}